(ns pe.primes
  (:use pe.digits))

(defn prime? [num]
  (cond
    (<= num 1) false
    (> num 1) (every? #(not (divides? num %))
                (range 2 (inc (int (Math/sqrt num)))))))

(defn primes-from [num]
  (filter prime? (iterate inc num)))

(def primes (primes-from 2))

(defn factor? [x y]
  (zero? (rem x y)))

(defn prime-factors [x primes factors]
  (let [firstprime (first primes)
        tailprimes (rest primes)]
    (cond
      (< x firstprime) factors
      (factor? x firstprime) (prime-factors (/ x firstprime) primes (conj factors firstprime))
      :else (prime-factors x tailprimes factors))))
